﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NB.MGJ.Common;
using System.Configuration;
using WxPayAPI;
using Common;
using NB.MGJ.Model;
using WebAppModel;
using NB.MGJ.Service;
using NB.MGJ.Service.Config;
using NB.MGJ.Service.AntiFake;
using System.Data;

namespace NB.MGJ.WebWap.Controllers
{
    public class WXController : Controller
    {
        JsApiPay jsApiPay = new JsApiPay();
        ManagerService _manager = new ManagerService();
        ConfigManagerService _wxService = new ConfigManagerService();
        MarketService _MarketService = new MarketService();

        /// <summary>
        ///用登录用户IP 
        /// </summary>
        protected string UserIp
        {
            get
            {

                string userIP = string.Empty;

                try
                {
                    if (System.Web.HttpContext.Current == null
                     || System.Web.HttpContext.Current.Request == null
                     || System.Web.HttpContext.Current.Request.ServerVariables == null)
                    {
                        return "";
                    }

                    string CustomerIP = "";

                    CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
                    if (!string.IsNullOrEmpty(CustomerIP))
                    {
                        return CustomerIP;
                    }

                    CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

                    if (!String.IsNullOrEmpty(CustomerIP))
                    {
                        return CustomerIP;
                    }

                    if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
                    {
                        CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

                        if (CustomerIP == null)
                        {
                            CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                        }
                    }
                    else
                    {
                        CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                    }

                    if (string.Compare(CustomerIP, "unknown", true) == 0 || String.IsNullOrEmpty(CustomerIP))
                    {
                        return System.Web.HttpContext.Current.Request.UserHostAddress;
                    }
                    return CustomerIP;
                }
                catch { }

                return userIP;
            }
        }


        // GET: Default
        public ActionResult Index()
        {
            try
            {

                string fwCode = Request["fwcode"];
                WeiXinHelper.WriteLog("fwCode", fwCode);
                AntiFakeVerifyService _AntiFakeVerifyService = new AntiFakeVerifyService();
                DataTable dt = _AntiFakeVerifyService.GetCodeModel(fwCode, "Fw_CodeSecret");
                UserScanQRInfoModel user = new UserScanQRInfoModel();
                if (dt != null)
                {
                    user.JiemiCode = dt.Rows[0]["Fw_Code"].ToString();
                    user.Eid = int.Parse(dt.Rows[0]["EnterpriseId"].ToString());
                    user.eCode = (_manager.GetEnterpriseModel_Eid(user.Eid) as Organization_Enterprise).Code;
                    user.BatchId = user.JiemiCode.Substring(4, AppConst.SubBatchLen);
                    user.ip = UserIp;
                    user.Code = fwCode;
                    Session["user"] = user;
                    config_WX wxModel = _wxService.GetwxBindModel(user.Eid);
                    wxModel.CredentialAddress = WxPayConfig.certPath + wxModel.CredentialAddress;
                    Session["wxConfig"] = wxModel;//微信配置信息
                    string appid = wxModel.AppId;
                    string secret = wxModel.AppSecret;
                  string url = WeiXinHelper.OAuthWeixin(appid, "https://www.m12315.com/app/Template/ActivityHome");
                    //string url = WeiXinHelper.OAuthWeixin(appid, "https://tapp.m12315.com/wx/ActivityHome");
                    Response.Redirect(url);
                }
                else
                {
                    return View("../PreView/WithoutView");
                }
            }
            catch (Exception ex)
            {
                WeiXinHelper.WriteLog("wx_index错误", ex.ToString());
            }
            return View();
        }

        public ActionResult PreHome()
        {
            try
            {
           
                int aId = Request["aId"] == null ? 0 : int.Parse(Request["aId"]);
                var model = _MarketService.GetActivityById(aId);
                config_WX wxModel = _wxService.GetwxBindModel(model.EnterpriseId.Value);
                
                Session["wxConfig"] = wxModel;//微信配置信息
               string url = WeiXinHelper.OAuthWeixin(wxModel.AppId, "https://www.m12315.com/app/PreView/ActivityHome?aId=" + aId);
               // string url = WeiXinHelper.OAuthWeixin(wxModel.AppId, "https://tapp.m12315.com/PreView/ActivityHome?aId=" + aId);
                Response.Redirect(url);
            }
            catch (Exception ex)
            {
                WeiXinHelper.WriteLog("wx_index错误", ex.ToString());
            }
            return View();
        }
        public string InfoActive(string signature, string timestamp, string nonce, string echostr)
        {
            return echostr;
        }

    }
}